home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
jwpsrc.zip
/
GLOSSARY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
23KB
|
730 lines
/* Copyright (C) Stephen Chung, 1991-1993. All rights reserved. */
#include "jwp.h"
#include "idm.h"
typedef struct {
KANJI far *shortcut;
KANJI far *phrase;
BOOL dynamic;
BOOL hidden;
} GLOSSARY;
static GLOSSARY Glossary[MAXGLOSSARY];
static int NrGlossary = 0;
static BOOL GlossaryChanged = FALSE;
static BOOL sizing = FALSE;
static KANJI far *CurrentShortCut;
static KANJI far *CurrentPhrase;
static KANJI far *ConvertGlossary (int id, LONG lParam, KANJI *buf)
{
return (Glossary[lParam].shortcut);
}
static KANJI far *ConvertGlossary1 (int id, LONG lParam, KANJI *buf)
{
return (Glossary[lParam].phrase);
}
static int GlossaryComp (void const *v1, void const *v2)
{
GLOSSARY *p1, *p2;
p1 = (GLOSSARY *) v1;
p2 = (GLOSSARY *) v2;
return (kanjicmp(p1->shortcut, p2->shortcut));
}
void ReadGlossary (char *filename)
{
int i, j, len, fd;
OFSTRUCT of;
/* Clear the old glossary */
for (i = 0; i < NrGlossary; i++) {
if (Glossary[i].shortcut != NULL) FreeBlock(Glossary[i].shortcut);
if (Glossary[i].phrase != NULL) FreeBlock(Glossary[i].phrase);
Glossary[i].shortcut = Glossary[i].phrase = NULL;
}
fd = OpenFile(filename, &of, OF_READ);
if (fd < 0) return;
/* Read the glossary file */
lseek(fd, 0L, 0);
read(fd, &NrGlossary, sizeof(int));
for (i = 0; i < NrGlossary; i++) {
/* Read the options */
read(fd, &j, sizeof(int));
Glossary[i].dynamic = j & 0x0001;
Glossary[i].hidden = j & 0x0002;
/* Read the short-cut */
read(fd, &len, sizeof(int));
len *= sizeof(KANJI);
Glossary[i].shortcut = BlockAlloc(len);
if (Glossary[i].shortcut == NULL) break;
_lread(fd, (char far *) Glossary[i].shortcut, len);
/* Read the phrase */
read(fd, &len, sizeof(int));
len *= sizeof(KANJI);
Glossary[i].phrase = BlockAlloc(len);
if (Glossary[i].shortcut == NULL) break;
_lread(fd, (char far *) Glossary[i].phrase, len);
}
close(fd);
if (NrGlossary > 0) {
qsort(Glossary, NrGlossary, sizeof(GLOSSARY), GlossaryComp);
}
}
void WriteGlossary (char *filename)
{
int i, j, len, fd;
OFSTRUCT of;
if (!GlossaryChanged) return;
fd = OpenFile(filename, &of, OF_WRITE | OF_CREATE);
if (fd < 0) {
ErrorMessage(global.hwnd, "Cannot create glossary file %s!", filename);
return;
}
for (i = j = 0; i < NrGlossary; i++) {
if (Glossary[i].shortcut != NULL) j++;
}
lseek(fd, 0L, 0);
write(fd, &j, sizeof(int));
for (i = 0; i < NrGlossary; i++) {
if (Glossary[i].shortcut == NULL) continue;
/* Write the options flag */
j = 0;
if (Glossary[i].dynamic) j |= 0x0001;
if (Glossary[i].hidden) j |= 0x0002;
write(fd, &j, sizeof(int));
/* Write the short-cut */
len = kanjilen(Glossary[i].shortcut) + 1;
write(fd, &len, sizeof(int));
len *= sizeof(KANJI);
_lwrite(fd, (char far *) Glossary[i].shortcut, len);
/* Write the phrase */
len = kanjilen(Glossary[i].phrase) + 1;
write(fd, &len, sizeof(int));
len *= sizeof(KANJI);
_lwrite(fd, (char far *) Glossary[i].phrase, len);
}
close(fd);
}
int SearchGlossary (KANJI far *key)
{
int len;
int top, bottom, middle;
int diff;
if (NrGlossary <= 0) return (-2);
/* Now binary search */
top = 0;
bottom = NrGlossary - 1;
len = kanjilen(key);
if (!kanjicmp(key, Glossary[top].shortcut)) {
if (Glossary[top].dynamic) return (top);
} else if (!kanjincmp(key, Glossary[top].shortcut, len)) {
if (Glossary[top].dynamic) return (-1);
}
if (!kanjicmp(key, Glossary[bottom].shortcut)) {
return (Glossary[bottom].dynamic ? bottom : -2);
} else if (!kanjincmp(key, Glossary[bottom].shortcut, len)) {
return (Glossary[bottom].dynamic ? -1 : -2);
}
for (;;) {
middle = (top + bottom) / 2;
diff = kanjicmp(key, Glossary[middle].shortcut);
if (diff == 0) {
return (Glossary[middle].dynamic ? middle : -2);
}
if (top >= bottom - 1) {
for (; bottom < NrGlossary; bottom++) {
diff = kanjincmp(key, Glossary[bottom].shortcut, len);
if (!Glossary[bottom].dynamic || diff != 0) continue;
return (-1);
}
return (-2);
}
if (diff > 0) top = middle;
else bottom = middle;
}
}
KANJI far *GetGlossary (int index)
{
return (Glossary[index].phrase);
}
BOOL FAR PASCAL EditGlossaryProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
switch (message) {
case WM_INITDIALOG: {
int i;
FILEOPTIONS *f;
KANJI buf[BUFSIZE];
/* Set the type and mode-change icon */
SendDlgItemMessage(hwnd, 4201, EM_SETMODIFY, FN_CONTROL | FN_NOKANJI, 0L);
SendDlgItemMessage(hwnd, 4201, EM_SETRECT, GetDlgItem(hwnd, 4211), 0L);
SendDlgItemMessage(hwnd, 4202, EM_SETMODIFY, FN_CONTROL, 0L);
SendDlgItemMessage(hwnd, 4202, EM_SETRECT, GetDlgItem(hwnd, 4211), 0L);
f = (FILEOPTIONS *) SendDlgItemMessage(hwnd, 4201, EM_GETHANDLE, 0, 0L);
SendDlgItemMessage(hwnd, 4211, EM_SETHANDLE, f->hwnd, 0L); /* mode-change icon */
if (CurrentShortCut != NULL) {
SendDlgItemMessage(hwnd, 4201, EM_REPLACESEL, 0, (LONG) CurrentShortCut);
SetFocus(GetDlgItem(hwnd, 4202));
}
if (CurrentPhrase != NULL) {
/* Edit */
SendDlgItemMessage(hwnd, 4202, EM_REPLACESEL, 0, (LONG) CurrentPhrase);
i = kanjilen(CurrentPhrase);
SendDlgItemMessage(hwnd, 4202, EM_SETSEL, i, MAKELONG(0, i-1));
SetWindowText(GetDlgItem(hwnd, 1), "&Done!");
SetWindowText(hwnd, "Edit an Existing Glossary Item");
} else {
/* Add */
SetWindowText(GetDlgItem(hwnd, 1), "&Add!");
SetWindowText(hwnd, "Add a Glossary Item");
}
CenterDialogBox(hwnd);
return (TRUE);
}
case WM_COMMAND:
switch (wParam) {
case IDCANCEL:
CurrentShortCut = CurrentPhrase = NULL;
EndDialog(hwnd, FALSE);
return (TRUE);
case IDOK: {
int i;
UNIT far *up, far *up1;
KANJI ch;
up1 = (UNIT far *) SendDlgItemMessage(hwnd, 4202, EM_GETLINE, 0, 0L);
if (unitlen(up1) <= 0) {
ErrorMessage(hwnd, "Sorry, you must enter a phrase for the short-cut.");
SetFocus(GetDlgItem(hwnd, 4202));
return (TRUE);
}
up = (UNIT far *) SendDlgItemMessage(hwnd, 4201, EM_GETLINE, 0, 0L);
if (unitlen(up) <= 0) {
ErrorMessage(hwnd, "Sorry, you must enter a short-cut for the phrase.");
SetFocus(GetDlgItem(hwnd, 4201));
return (TRUE);
}
CurrentShortCut = (KANJI far *) BlockAlloc((unitlen(up) + 5) * sizeof(KANJI));
for (i = 0; up[i].kanji; i++) CurrentShortCut[i] = up[i].kanji;
CurrentShortCut[i] = 0;
CurrentPhrase = (KANJI far *) BlockAlloc((unitlen(up1) + 5) * sizeof(KANJI));
for (i = 0; up1[i].kanji; i++) CurrentPhrase[i] = up1[i].kanji;
CurrentPhrase[i] = 0;
EndDialog(hwnd, TRUE);
return (TRUE);
}
}
return (TRUE);
case WM_PAINT: {
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
DrawBoundingBox(hwnd, hd